//
//  SubjectController.m
//  TravelNotes
//
//  Created by qianfeng on 15-7-7.
//  Copyright (c) 2015年 ZWB. All rights reserved.
//

#import "SubjectController.h"
#import "AFNetworking.h"
#import "SubjectCell.h"

@interface SubjectController () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
{
    AFHTTPRequestOperationManager *_manager;
}

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation SubjectController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initUI];
    [self firstDownload];
}

- (void)initUI {
    self.dataArray = [[NSMutableArray alloc] init];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(kScreenSize.width, 220);
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 5;
    flowLayout.sectionInset = UIEdgeInsetsMake(2, 2, 2, 2);
//    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width, kScreenSize.height-100) collectionViewLayout:flowLayout];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    [self.view addSubview:self.collectionView];
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"SubjectCell" bundle:nil] forCellWithReuseIdentifier:@"SubjectCell"];
    
    _manager = [AFHTTPRequestOperationManager manager];
    _manager.responseSerializer = [AFHTTPResponseSerializer serializer];
}

- (void)firstDownload {
    self.currentPage = 1;
    NSString *url = [NSString stringWithFormat:kSubjectUrl, self.currentPage];
    [self addTaskWithUrl:url isRefresh:NO];
}

- (void)addTaskWithUrl:(NSString *)url isRefresh:(BOOL)isRefresh {
//    __weak typeof(self) weakSelf = self;
    [_manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"下载成功");
        if (responseObject) {
            NSArray *array = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            for (NSDictionary *dict in array) {
                SubjectModel *model = [[SubjectModel alloc] init];
                [model setValuesForKeysWithDictionary:dict];
                [self.dataArray addObject:model];
            }
            
            [self.collectionView reloadData];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"下载失败");
    }];
}

#pragma mark - delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SubjectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SubjectCell" forIndexPath:indexPath];
    SubjectModel *model = self.dataArray[indexPath.row];
    
    [cell showDataWithModel:model];
    
    return cell;
}

@end
